package com.bd.scm.module.role.controller;


import com.alibaba.fastjson.JSONObject;
import com.bd.scm.domain.dto.role.BdUserAdminDto;
import com.bd.scm.domain.dto.role.BdUserAdminParam;
import com.bd.scm.module.common.BaseController;
import com.bd.scm.module.role.service.BdUserAdminService;
import com.bd.scm.module.utils.ResultUtil;
import com.bd.utils.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Mason
 * @since 2021-01-11
 */
@RestController
@RequestMapping("/api/bdUserAdmin")
public class BdUserAdminController extends BaseController {

    @Autowired
    private BdUserAdminService bdUserAdminService;

    /**
     * 查询管理员账户.
     *
     * @param bdUserAdminParam
     * @return
     */
    @PostMapping("getAccountList")
    public Result getAccountList(@RequestBody BdUserAdminParam bdUserAdminParam) {
        return ResultUtil.ok(bdUserAdminService.getAccountList(bdUserAdminParam));
    }

    /**
     * 新增或更改账户.
     *
     * @param bdUserAdminDto
     * @return
     * @throws Exception
     */
    @PostMapping("addOrUpdateUser")
    public Result addOrUpdateUser(@RequestBody BdUserAdminDto bdUserAdminDto) throws Exception {
        Result result = null;
        Long loginer = getLoginUserId();
        if (loginer != 0L) {
            JSONObject r = bdUserAdminService.addOrUpdateUser(bdUserAdminDto, Integer.parseInt(loginer.toString()));
            if (null == r) {
                result = ResultUtil.ok("保存成功");
            } else {
                result = ResultUtil.error(r.getString("message"));
            }
        } else {
            result = ResultUtil.error("登陆超时，请重新登录!");
        }
        return result;
    }

    /**
     * 删除管理员账户.
     *
     * @param bdUserAdminParam
     * @return
     */
    @PostMapping("deleteAccount")
    public Result deleteAccount(@RequestBody BdUserAdminParam bdUserAdminParam) {
        Result result = null;
        Long loginer = getLoginUserId();
        if (loginer != 0L) {
            bdUserAdminService.deleteAccount(bdUserAdminParam, Integer.parseInt(loginer.toString()));
            result = ResultUtil.ok("删除成功");
        } else {
            result = ResultUtil.error("登陆超时，请重新登录!");
        }
        return result;
    }
}

